#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import json
import sys

from common.common import exter_attack
from common.exception.common_exception import ErrCodeException
from saphana.backup.saphana_backup_service import BackupService
from common.const import SysData
from generaldb.saphana.comm.common_util import log
from saphana.resource.saphana_resource_service import SaphanaResourceService
from saphana.restore.saphana_restore_service import SaphanaRestoreService


class SaphanaApi:

    @staticmethod
    @exter_attack
    def check_backup_job_type(pid, job_id, sub_job_id):
        backup_service = BackupService(pid, job_id, sub_job_id)
        return backup_service.check_backup_type()

    @staticmethod
    @exter_attack
    def query_job_permission(pid, job_id, sub_job_id):
        return BackupService.query_job_permission(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def allow_backup_in_local_node(pid, job_id, sub_job_id):
        backup_service = BackupService(pid, job_id, sub_job_id)
        return backup_service.allow_backup_in_local_node()

    @staticmethod
    @exter_attack
    def backup_prerequisite(pid, job_id, sub_job_id):
        backup_service = BackupService(pid, job_id, sub_job_id)
        return backup_service.exec_prerequisite_task()

    @staticmethod
    @exter_attack
    def backup_gen_sub_job(pid, job_id, sub_job_id):
        backup_service = BackupService(pid, job_id, sub_job_id)
        return backup_service.generate_subjob()

    @staticmethod
    @exter_attack
    def backup(pid, job_id, sub_job_id):
        backup_service = BackupService(pid, job_id, sub_job_id)
        return backup_service.exec_subjob()

    @staticmethod
    @exter_attack
    def backup_post_job(pid, job_id, sub_job_id):
        backup_service = BackupService(pid, job_id, sub_job_id)
        backup_service.exec_post_job()
        return True

    @staticmethod
    @exter_attack
    def abort_job(pid, job_id, sub_job_id):
        backup_service = BackupService(pid, job_id, sub_job_id)
        backup_service.abort_backup()
        return True

    @staticmethod
    @exter_attack
    def allow_restore_in_local_node(pid, job_id, sub_job_id):
        restore_service = SaphanaRestoreService(pid, job_id, sub_job_id)
        return restore_service.allow_restore_in_local_node()

    @staticmethod
    @exter_attack
    def restore_prerequisite(pid, job_id, sub_job_id):
        restore_service = SaphanaRestoreService(pid, job_id, sub_job_id)
        return restore_service.restore_prerequisite()

    @staticmethod
    @exter_attack
    def restore(pid, job_id, sub_job_id):
        restore_service = SaphanaRestoreService(pid, job_id, sub_job_id)
        return restore_service.restore()

    @staticmethod
    @exter_attack
    def restore_post_job(pid, job_id, sub_job_id):
        restore_service = SaphanaRestoreService(pid, job_id, sub_job_id)
        return restore_service.restore_post_job()

    @staticmethod
    @exter_attack
    def restore_gen_sub_job(pid, job_id, sub_job_id):
        restore_service = SaphanaRestoreService(pid, job_id, sub_job_id)
        return restore_service.restore_gen_sub_job()

    @staticmethod
    @exter_attack
    def deliver_task_status(pid, job_id, sub_job_id):
        restore_service = SaphanaRestoreService(pid, job_id, sub_job_id)
        return restore_service.deliver_task_status()

    @staticmethod
    @exter_attack
    def list_application_v2(pid, job_id, sub_job_id):
        SaphanaResourceService(pid, job_id, sub_job_id).list_application_v2()


func_dict = {
    'AllowBackupInLocalNode': SaphanaApi.allow_backup_in_local_node,
    'CheckBackupJobType': SaphanaApi.check_backup_job_type,
    'QueryJobPermission': SaphanaApi.query_job_permission,
    'BackupPrerequisite': SaphanaApi.backup_prerequisite,
    'BackupGenSubJob': SaphanaApi.backup_gen_sub_job,
    'Backup': SaphanaApi.backup,
    'BackupPostJob': SaphanaApi.backup_post_job,
    'AbortJob': SaphanaApi.abort_job,
    'PauseJob': SaphanaApi.abort_job,
    'AllowRestoreInLocalNode': SaphanaApi.allow_restore_in_local_node,
    'RestorePrerequisite': SaphanaApi.restore_prerequisite,
    'RestoreGenSubJob': SaphanaApi.restore_gen_sub_job,
    'Restore': SaphanaApi.restore,
    'RestorePostJob': SaphanaApi.restore_post_job,
    'DeliverTaskStatus': SaphanaApi.deliver_task_status,
    'ListApplicationResourceV2': SaphanaApi.list_application_v2
}


def saphana_main(hana_args):
    """
    功能描述：主任务执行
    参数：
    @fun_type：argv[0] 方法类型
    @pid：argv[1] 请求ID
    @job_id：argv[2] 主任务任务ID
    @sub_job_id: argv[3] 子任务ID
    例：python3 api.py fun_type pid job_id sub_job_id
    """
    fun_type = hana_args[0]
    pid = hana_args[1]
    job_id = ''
    sub_job_id = ''
    if len(hana_args) > 2:
        job_id = hana_args[2]
    if len(hana_args) > 3:
        sub_job_id = hana_args[3]
    log.info(f'######## function: {fun_type} start, pid: {pid}, job_id: {job_id}, sub_job_id: {sub_job_id}')
    try:
        func_dict.get(fun_type)(pid, job_id, sub_job_id)
    except ErrCodeException as errcode_str:
        log.exception(errcode_str)
        log.error(f'######## function: {fun_type} exception. {errcode_str}', exc_info=1)
    except Exception as exception_str:
        log.exception(exception_str)
        log.error(f'######## function: {fun_type} exception. {exception_str}', exc_info=1)
    log.info(f'######## function: {fun_type} end, pid: {pid}')


if __name__ == '__main__':
    for line in sys.stdin:
        SysData.SYS_STDIN = line
        break
    args = sys.argv[1:]
    saphana_main(args)
